﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Query.SupplierModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Domain.Query.MaterialModule;
using Enterprise.Framework.Frame.Architecture;

namespace Resto.CostControl.Core.Facade.SupplierModule.FacadeObject
{
    /// <summary>
    /// 供应商合同FO
    /// </summary>
    public class SupplierContractFO : IApplicationFacadeObject
    {
        #region 私有属性

        private SupplierContractDQO _SupplierContractDQO;
        private SupplierContractDQO SupplierContractDQO
        {
            get
            {
                if (_SupplierContractDQO == null)
                    _SupplierContractDQO = ObjectCreator.Create<SupplierContractDQO>();

                return _SupplierContractDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddSupplierContract(SupplierContractDTO contractDTO)
        {
            SupplierContract supplierContract = contractDTO.ToSupplierContract();
            supplierContract.Add();
            
            RestoCostDomainSession.CurrentSession.SaveChanges();

            contractDTO.ID = supplierContract.ID;
        }

        public virtual void UpdateSupplierContract(SupplierContractDTO contractDTO)
        {
            SupplierContract supplierContract = SupplierContractDQO.GetSupplierContract(contractDTO.ID);
            contractDTO.CopyToSupplierContract(supplierContract);

            supplierContract.Update();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteSupplierContract(string id)
        {
            SupplierContract supplierContract = SupplierContractDQO.GetSupplierContract(id);

            supplierContract.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual SupplierContractDTO GetSupplierContractDTO(string id)
        {
            SupplierContract supplierContract = SupplierContractDQO.GetSupplierContract(id);

            return new SupplierContractDTO(supplierContract);
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 生成材料的价格
        /// </summary>
        private void CreateMaterialPrice(int materialId)
        {
            //MaterialDQO materialDQO = ObjectCreator.Create<MaterialDQO>();
            //Material material = materialDQO.GetMaterial(materialId);

            //SupplierContract contract = SupplierContractDQO.GetNewSupplierContract(materialId);
            //if (contract == null)
            //    return;

            //if (material.CurrentPrice == contract.Price)
            //    return;

            ////更新原材料的的价格
            //material.UpdatePrice(contract.Price);

            //RestoCostDomainSession.CurrentSession.SaveChanges();
        }

        #endregion
    }
}
